package loc.dd.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;

import loc.dd.entity.Abilities;
import loc.dd.entity.Permissions;
import loc.dd.entity.User;
import loc.dd.service.UserService;
import net.sf.json.JSONObject;

/**
 * 
 * @author hudage
 *
 */
@RestController
@RequestMapping("/user")
public class UserController {
	private static Logger logger =LoggerFactory.getLogger(UserController.class);
	
	@Autowired
	private UserService userservice;
	
	/**
	 * 登陆验证
	 * @param request
	 * @param response
	 * @return
	 */
	@CrossOrigin
	@RequestMapping(value = "login" , method = RequestMethod.POST)
	public Map<String,Object> login(@RequestBody JSONObject params,HttpServletRequest request,HttpServletResponse response) {
		JSONArray jo = new JSONArray();
		JSONObject jsonx = (JSONObject) params.get("logindata");
		Map<String, Object> map = new HashMap<>();
		String userphone=(String)jsonx.get("userphone");
		String password=(String)jsonx.get("password");
		int c = userservice.checklogin(userphone);
		System.out.println("登陆拉+++++++++++"+userphone+"----------------"+password);
		if(c == 0) {
			map.put("status", "407");//用户不存在
			return map;
		}
		int a = userservice.getlogin(userphone,password);
		if(a == 2){
			map.put("status", "408");//密码错误
			return map;
		}else if(a == 3){
			map.put("status", "409");//账号已锁定
			return map;
		}else {
			map.put("status", "200");//登陆成功
			Map<String, Object> getloginuser= new HashMap<String, Object>();
			getloginuser = userservice.getloginuser(userphone);
			map.put("user", getloginuser);
			return map;
		}
		
    }
	
	/**
	 *查找全部人员
	 * @param request
	 * @param response
	 * @return
	 */
	@CrossOrigin
	@RequestMapping(value = "getuser" , method = RequestMethod.GET)
	public JSONArray getjs(HttpServletRequest request,HttpServletResponse response) {
		JSONArray jo = new JSONArray();
		String pageSize = request.getParameter("pageSize");
		String page = request.getParameter("page");
		int a = 0;
		int b = 10000;
		if(null != page && !"".equals(page)) {
			if(null != pageSize && !"".equals(pageSize)) {
				a = Integer.parseInt(page);
				b = Integer.parseInt(pageSize);
				if(a==1) {
					a=0;
				}else {
				 a=b*(a-1);
				}
			}
		}
		List<Map<String, Object>> ab= new ArrayList<Map<String, Object>> ();
		ab = userservice.getuser(a,b);
//		ab = userservice.getuser(0,1000);
		int sum = userservice.getsum();
		System.out.println(ab);
		jo.add(ab);	
		jo.add(sum);
		System.out.println(sum);
		return jo;
    }
	
	
	/**
	 * 编辑人员
	 * @param params
	 * @param request
	 * @param response
	 * @return
	 */
	@CrossOrigin
	@RequestMapping(value = "edituser" , method = RequestMethod.POST)
	public Map<String,Object> editnl(@RequestBody JSONObject params,HttpServletRequest request,HttpServletResponse response) {
		JSONObject jsonx = (JSONObject) params.get("data");
		System.out.println("edituser");
		Map<String, Object> map = new HashMap<>();
		String username=(String)jsonx.get("username");
		String userphone=(String)jsonx.get("userphone");
		String password=(String)jsonx.get("password");
		String description=(String)jsonx.get("description");
		String rid=(String)jsonx.get("rid");
		String aid=(String)jsonx.get("aid");
		String state=(String)jsonx.get("state");
		int isadd = (int) jsonx.get("isAdd");
		int isedit = (int) jsonx.get("isEdit");
		if(isedit == 0) {
			aid = "999";
		}
		int isend = (int) jsonx.get("isEnd");
		User us = new User();
		us.setUsername(username);
		us.setDescription(description);
		us.setUserphone(userphone);
		us.setPassword(password);
		if(aid == null || "".equals(aid)) {
			aid = "999";
		}
		us.setAid(aid);
		us.setRid(rid);
		us.setIsAdd(isadd);
		us.setIsEdit(isedit);
		us.setIsEnd(isend);
		us.setState(state);
		if("" != jsonx.get("id") && !"".equals(jsonx.get("id")) && jsonx.get("id") !=null){
			int ids = (int) jsonx.get("id");
			int a = userservice.getoneuser(userphone,ids);
			if(a>0) {
				map.put("status", "407");
				return map;
			}
			us.setId(ids);
			userservice.edituser(us);
			Map<String, Object> newuser= new HashMap<String, Object>();
			newuser = userservice.getnewuser(ids);
			map.put("status", "200");
			map.put("user", newuser);
			return map; 
		}else {
			us.setDescription(description);
			int a = userservice.checkshifouuser(userphone);
			if(a>0) {
				map.put("status", "407");
				return map;
			}
			Date currentTime = new Date();
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String dateString = formatter.format(currentTime);
			us.setLastlogin(dateString);
			userservice.adduser(us);
			Map<String, Object> newuser= new HashMap<String, Object>();
			newuser = userservice.getnewuser(us.getId());
			map.put("status", "200");
			map.put("user", newuser);
			System.out.println(newuser);
			return map; 
		}
    }
	
	/**
	 * 模糊查找
	 * @param request
	 * @param response
	 * @return
	 */
	@CrossOrigin
	@RequestMapping(value = "selectuser" , method = RequestMethod.GET)
	public JSONArray selectuser(HttpServletRequest request,HttpServletResponse response) {
		JSONArray jo = new JSONArray();
		String username = request.getParameter("username");
		List<Map<String, Object>> ab= new ArrayList<Map<String, Object>> ();
		String pageSize = request.getParameter("pageSize");
		String page = request.getParameter("page");
		System.out.println("selectuser");
		int a = 0;
		int b = 10000;
		if(null != page && !"".equals(page)) {
			if(null != pageSize && !"".equals(pageSize)) {
				a = Integer.parseInt(page);
				b = Integer.parseInt(pageSize);
				if(a==1) {
					a=0;
				}else {
				 a=b*(a-1);
				}
			}
		}
		ab = userservice.selectuser(username,a,b);
		int sum = userservice.selectsum(username);
		System.out.println(ab);
		jo.add(ab);	
		jo.add(sum);
		System.out.println(sum);
		return jo;
    }

	
}
